package com.hileto.inf.actions.methods;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.hileto.inf.actions.filters.RestFilter;
import com.hileto.inf.actions.methods.params.SysParam;
import com.hileto.inf.utils.GenericsUtils;
import com.hileto.inf.utils.RequestBeanUtils;

/**
 * Copyright: Copyright (c) 2012 深圳市海乐淘电子商务有限公司
 * 
 * @Author : 曾林宝
 * @CreateTime : 2013-1-21 上午10:19:51
 * 
 * @Description: 所有请求处理的基类,用于开放平台.
 */
public abstract class BaseMethod<T> implements InitializingBean {
	@Autowired
	protected RestFilter restFilter;
	
	private Log logger = LogFactory.getLog(getClass());

	protected abstract String getMethodName();

	private Class<T> paramBeanClass;
	

	
	@Override
	@SuppressWarnings("all")
	public final void afterPropertiesSet() throws Exception {
		restFilter.registerMethod(getMethodName(), this);
		paramBeanClass = GenericsUtils.getSuperClassGenricType(getClass());
	}

	/**
	 * @Description : 处理请求
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-22 下午02:30:38
	 * @param sysParam
	 * @param request
	 * @return
	 */
	public abstract String doHttpProcess(SysParam sysParam,
			HttpServletRequest request);

	/**
	 * @Description : 生成个性参数.
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-22 下午02:57:52
	 * @param request
	 * @return
	 */
	public T getParamBean(HttpServletRequest request) {
		T paramBean = null;
		try {
			paramBean = paramBeanClass.newInstance();
			RequestBeanUtils.getValuesByRequest(request, paramBean);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return paramBean;
	}
}
